import 'package:enterprise_v6_app/app/components/custom_modele/custom_card.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/common/charts_pie.dart';
import 'package:enterprise_v6_app/app/modules/application/emergency/home/home_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';
import 'package:syncfusion_flutter_charts/charts.dart';

class EmergencyHomePage extends StatelessWidget {
  EmergencyHomePage({super.key});

  final controller = Get.put(EmergencyHomeController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      initState: (_) {
        controller.initData();
      },
      builder: (_) {
        return Scaffold(
          body: Container(
            alignment: Alignment.topCenter,
            color: const Color(0xffF5F5F6),
            child: ListView(
              padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 14.w),
              children: _build(),
            ),
          ),
          bottomSheet: _buildBottom(),
        );
      },
    );
  }

  /// 构建页面
  List<Widget> _build() {
    return [
      // _buildOnlineChamber(),
      // AppGaps.vGap10,
      _buildEmergencyPlan(),
      AppGaps.vGap10,
      _buildEmergencyDrill(),
      AppGaps.vGap10,
      _buildEmergencyResource(),
      AppGaps.vGap50,
    ];
  }

  /// 在线商会
  Widget _buildOnlineChamber() {
    return CustomCard(
      divider: CustomCard.builtinDividerWithPadding,
      title: '在线商会',
      titleRight: Row(
        children: [
          Text(
            '立即进入',
            style: TextStyle(color: Colors.blue, fontSize: 12.w),
          ),
          AppGaps.hGap6,
          SvgPicture.asset(AppImages.icon_job_right),
          AppGaps.hGap4,
        ],
      ),
      content: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                '主题：',
                style: TextStyle(
                  color: const Color(0xFFA5A6B5),
                  fontWeight: FontWeight.w500,
                  fontSize: 13.w,
                ),
              ),
              Text(
                'xxxx',
                style: TextStyle(
                  color: const Color(0xFF495061),
                  fontSize: 12.w,
                ),
              ),
            ],
          ),
          AppGaps.vGap10,
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                '时间：',
                style: TextStyle(
                  color: const Color(0xFFA5A6B5),
                  fontWeight: FontWeight.w500,
                  fontSize: 13.w,
                ),
              ),
              Text(
                '2023/11/12 12:00-2023/11/12 14:00',
                style: TextStyle(
                  color: const Color(0xFF495061),
                  fontSize: 12.w,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  /// 企业应急预案
  Widget _buildEmergencyPlan() {
    return CustomCard(
      divider: CustomCard.builtinDividerWithPadding,
      title: '企业应急预案',
      content: Column(
        children: [
          _buildEmergencyPlanItem('综合应急预案', controller.allPlanEntity.comprehensiveNum.toString()),
          AppGaps.vGap12,
          _buildEmergencyPlanItem('专项应急预案', controller.allPlanEntity.specialNum.toString()),
          AppGaps.vGap12,
          _buildEmergencyPlanItem('现场处置方案', controller.allPlanEntity.sceneLiveNum.toString()),
          AppGaps.vGap12,
          _buildEmergencyPlanItem('特殊时期/重大活动应急预案', controller.allPlanEntity.majorNum.toString()),
        ],
      ),
    );
  }

  Widget _buildEmergencyPlanItem(String name, String value) {
    return Container(
      decoration: BoxDecoration(
        color: const Color(0x1A5C6DFF),
        borderRadius: BorderRadius.circular(7.w),
      ),
      padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 4.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            name,
            style: TextStyle(color: const Color(0xFF3141CD), fontSize: 14.w),
          ),
          AppGaps.hGap10,
          Text(
            value,
            style: TextStyle(color: const Color(0xFF3141CD), fontSize: 14.w),
          ),
        ],
      ),
    );
  }

  /// 应急演练
  Widget _buildEmergencyDrill() {
    return CustomCard(
      divider: CustomCard.builtinDividerWithPadding,
      title: '应急演练',
      content: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          _buildEmergencyDrillItem(
            '预案演示',
            controller.allDrillEntity.planDemonstrationNum.toString(),
            controller.allDrillEntity.lastYearPlanDemonstrationNum,
          ),
          _buildEmergencyDrillItem(
            '协同演练',
            controller.allDrillEntity.collaborativeExerciseNum.toString(),
            controller.allDrillEntity.lastYearCollaborativeExerciseNum,
          ),
          _buildEmergencyDrillItem(
            '推演复盘',
            controller.allDrillEntity.inferenceReviewNum.toString(),
            controller.allDrillEntity.lastYearInferenceReviewNum,
          ),
        ],
      ),
    );
  }

  Widget _buildEmergencyDrillItem(String name, String value, int diff) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Container(
          width: 74.w,
          padding: EdgeInsets.symmetric(vertical: 4.w, horizontal: 12.w),
          decoration: BoxDecoration(
            color: const Color(0xFF5C6DFF),
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(7.w),
              topRight: Radius.circular(7.w),
            ),
          ),
          child: Text(
            name,
            style: TextStyle(
              color: Colors.white,
              fontSize: 12.w,
              fontWeight: FontWeight.w500,
            ),
          ),
        ),
        Container(
          decoration: BoxDecoration(
            color: const Color(0x1A5C6DFF),
            borderRadius: BorderRadius.only(
              bottomLeft: Radius.circular(7.w),
              bottomRight: Radius.circular(7.w),
            ),
          ),
          width: 74.w,
          child: Center(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                AppGaps.vGap6,
                Text(
                  value,
                  style: TextStyle(
                    color: const Color(0xFF5C6DFF),
                    fontSize: 16.w,
                    fontWeight: FontWeight.w500,
                  ),
                ),
                AppGaps.vGap6,
                Text(
                  '同去年对比',
                  style: TextStyle(color: const Color(0xFF5C6DFF), fontSize: 12.w),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    diff >= 0
                        ? SizedBox(
                            height: 10.w,
                            width: 10.w,
                            child: Image.asset(
                              AppImages.icon_green_increase_arrow,
                            ),
                          )
                        : SizedBox(
                            height: 10.w,
                            width: 10.w,
                            child: Image.asset(
                              AppImages.icon_red_decrease_arrow,
                            ),
                          ),
                    Text(
                      diff.toString(),
                      style: TextStyle(color: const Color(0xFF5C6DFF), fontSize: 12.w),
                    ),
                  ],
                ),
                AppGaps.vGap6,
              ],
            ),
          ),
        ),
      ],
    );
  }

  /// 应急资源
  Widget _buildEmergencyResource() {
    return CustomCard(
      divider: CustomCard.builtinDividerWithPadding,
      title: '应急资源',
      content: SizedBox(
        height: 190.w,
        child: SfCartesianChart(
          primaryXAxis: CategoryAxis(
            labelStyle: TextStyle(fontSize: 9.w, color: const Color(0xFF4F4F4F)),
          ),
          primaryYAxis: NumericAxis(
            interval: 50,
            labelStyle: TextStyle(
              color: const Color(0xFFA0A7B5),
              fontSize: 9.w,
            ),
            axisLine: const AxisLine(width: 0),
          ),
          plotAreaBorderWidth: 0,
          trackballBehavior: TrackballBehavior(
            enable: true,
            lineDashArray: const [10],
            lineColor: Colors.grey,
            lineWidth: 1,
            activationMode: ActivationMode.singleTap,
            tooltipDisplayMode: TrackballDisplayMode.groupAllPoints,
            tooltipSettings: const InteractiveTooltip(
              enable: true,
              color: Colors.white,
              borderColor: Colors.grey,
              connectorLineColor: Colors.transparent,
              textStyle: TextStyle(color: Colors.black),
            ),
          ),
          series: <ChartSeries<ChartData, String>>[
            ColumnSeries<ChartData, String>(
              dataSource: controller.resChartData,
              xValueMapper: (data, _) => data.x,
              yValueMapper: (data, _) => data.y,
              color: const Color.fromRGBO(8, 142, 255, 1),
            )
          ],
        ),
      ),
    );
  }

  Widget _buildBottom() {
    return SafeArea(
      child: Container(
        height: 50.w,
        decoration: BoxDecoration(
          color: Colors.white,
          boxShadow: [
            BoxShadow(
              color: const Color(0x0D000000),
              offset: const Offset(0, -3),
              blurRadius: 2.w,
              spreadRadius: 0.w,
            ),
          ],
          shape: BoxShape.rectangle,
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            AppGaps.hGap10,
            Flexible(
              flex: 2,
              child: Container(
                height: 40.w,
                decoration: BoxDecoration(
                  border: Border.all(color: AppColors.blue_button, width: 1.3.w),
                  borderRadius: BorderRadius.circular(37.w),
                ),
                child: Center(
                  child: Text(
                    '接警续报',
                    style: TextStyle(
                      fontSize: 16.w,
                      color: AppColors.blue_button,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ),
              ).clickOnce(() {
                Get.toNamed(AppRoutes.accidentResubmit);
              }),
            ),
            AppGaps.hGap16,
            Flexible(
              flex: 3,
              child: Container(
                height: 40.w,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(37.w),
                  color: AppColors.blue_button,
                ),
                child: Center(
                  child: Text(
                    '接警登记',
                    style: TextStyle(
                      fontSize: 16.w,
                      color: Colors.white,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ),
              ).clickOnce(() {
                Get.toNamed(AppRoutes.accidentRegister);
              }),
            ),
            AppGaps.hGap10,
          ],
        ),
      ),
    );
  }
}
